<template>
  <div class="allCom-page">
    <div class="all-top" v-if="!showLoading">
      <div class="left" @click="$router.go(-1)">
        <img src="../assets/images/back_b_icon.png" alt="" />
      </div>
      <div class="title">全部评论({{allComlists.length}})</div>
      <div class="right"></div>
    </div>
    <div class="all-comment" v-if="!showLoading">
      <div class="pl-item" v-for="(item) in allComlists" :key="item.id">
        <div class="pl-img">
          <img :src="'https://pic.ecook.cn/web/'+ item.userimageid+'.jpg!wl280'" alt="" />
        </div>
        <div class="pl-msg">
          <span class="msg-name">{{ item.username }}:</span>
          <span class="msg">{{ item.text }}</span>
        </div>
      </div>       
    </div>
    <div class="search">
      <input type="text" placeholder="我也来说两句吧" v-model="word" v-todo-focus><span @click="send">发送</span>
    </div>
    <van-loading size="24px" class="loading" v-if="showLoading" type="spinner">正在拼命加载中...</van-loading>
  </div>
</template>

<script>
import {getCommentListData} from '../api/deatils'
export default {
  data() {
    return {
      allComlists: [],
      showAllComflag:true,
      showLoading:true,
      word:'',
      userId:null,
    };
  },
   methods: {
    //获取评论总数据
    getCommentListFun(id,type){
      getCommentListData({id,type}).then(data=>{
        console.log(data);
        if (data) {
          this.showLoading = false
        }
        this.allComlists = data.list
        //重置评论
        let tempCommentList = JSON.parse(window.localStorage.getItem("commentList"))
        if (tempCommentList) {
            tempCommentList.forEach(item=>{
              if (item.contentId == this.$route.query.id) {
                  this.allComlists.unshift(item)
              }
            })
        }
      })
    },
    send(){
      if (this.word) {
        let userId = window.localStorage.getItem("userId");
        let tempCommentList = JSON.parse(window.localStorage.getItem("commentList"))
        let comment = {
        contentId:this.$route.query.id,
        userid:userId,
        username:'厨友'+userId,
        userimageid:"8877665544",
        text:this.word
      }
      let commentList =tempCommentList==null?[]:tempCommentList
      commentList.push(comment)
      window.localStorage.setItem("commentList",JSON.stringify(commentList))
      this.allComlists.unshift(comment)
      }
      this.word=''
    }
  },
  created() {
    // 获取菜谱评论总数据
    this.getCommentListFun(this.$route.query.id,'recipe')
    this.userId = window.localStorage.getItem("userId")
  },
  // 自定义指令
    directives:{
      "todo-focus":{     
        inserted:function(el){      
          // 获取焦点
          el.focus()
        }
      }
    },
};
</script>

<style lang="less" scoped>
.allCom-page {
  width: 100%;
  height: 100%;
  overflow-y: scroll;

  .all-top {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    height: 50px;
    display: flex;
    align-items: center;
    background-color: #fff;
    justify-content: space-between;
    border-bottom: 1px solid #ccc;
    box-sizing: border-box;
    .left {
      width: 12px;
      height: 22px;
      margin-left: 12px;
    }
    .title {
      font-weight: bold;
    }
    .right {
      width: 5%;
    }
  }
  .all-comment {
    margin-top: 70px;
    margin-bottom: 60px;
    padding: 0 20px;
    .pl-item {
      margin-bottom: 20px;
      display: flex;
      justify-content: flex-start;

      .pl-img {
        width: 27px;
        height: 27px;

        & > img {
          width: 27px;
          height: 27px;
          display: block;
          border-radius: 50%;
        }
      }

      .pl-msg {
        margin-left: 10px;
        display: flex;
        flex-direction: column;

        .msg-name {
          padding: 2px 0 10px 0;
          color: #000;
          font-size: 14px;
        }

        .msg {
          padding-top: 5px;
          color: #000;
          opacity: 0.6;
          font-size: 13px;
          font-weight: bold;
          line-height: 25px;
          letter-spacing: 0.8px;
        }
      }
    }
  }
  .search{
    width: 100%;
    height: 50px;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    align-items: center;
    justify-content: center;

    &>input{
      width: 70%;
      padding: 7px 15px;
      border-radius: 20px;
      font-size: 14px;
      letter-spacing: 0.8px;
      border: none;
      outline: none;
      background-color: #f2f2f2;
    }
    &>span{
      color: #f7cd35;
      font-size: 15px;
      margin-left: 10px;
      font-weight: bold;
    }
  }
  .van-loading{
    height: 500px;
    line-height: 500px;
    text-align: center;
  }
  .loading {
  position: fixed;
  top: 50px;
  left: 0;
  right: 0;
  bottom: 100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}
}
</style>